package com.ruicar.afs.cloud.apply.pre.app.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.pre.app.condition.*;
import com.ruicar.afs.cloud.apply.pre.app.entity.*;
import com.ruicar.afs.cloud.apply.pre.app.service.*;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * description: app管理平台
 * @date: 2020/8/7 14:28
 * @author mingzhi.li
 */
@Slf4j
@RestController
@RequestMapping("/appManagement")
@AllArgsConstructor
public class ApplyAppManagementController {

    private final ApplyAppCommonProblemService applyAppCommonProblemService;
    private final ApplyAppOpinionFeedbackService applyAppOpinionFeedbackService;
    private final ApplyAppPictureManagementService applyAppPictureManagementService;
    private final ApplyAppVersionInfoService applyAppVersionInfoService;
    private final TsysNoticePublishService tsysNoticePublishService;
    private final ComAttachmentFileService attachmentFileService;

    /**
     * 新增/修改
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveCommonProblem")
    @CacheEvict(value = "apply_app_common_problem", allEntries = true)
    @ApiOperation(value = "保存修改数据数据")
    public IResponse saveAppCommonProblem(@RequestBody ApplyAppCommonProblemCondition condition) {
        applyAppCommonProblemService.saveOrUpdate(condition);
        return IResponse.success("保存成功");
    }


    @PostMapping("/getAppCommonProblem")
    @ApiOperation(value = "分页查询app常见问题列表信息")
    public IResponse<IPage<ApplyAppCommonProblem>> getAppCommonProblem(@RequestBody ApplyAppCommonProblemCondition condition) {
        IPage<ApplyAppCommonProblem> list = applyAppCommonProblemService.page(new Page(condition.getPageNumber(), condition.getPageSize())
                , Wrappers.<ApplyAppCommonProblem>query().lambda()
                        .eq(StringUtils.isNotEmpty(condition.getProblemType()), ApplyAppCommonProblem::getProblemType, condition.getProblemType()));
        return IResponse.success(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delByIds/{ids}")
    @ApiOperation(value = "批量删除")
    public IResponse<Boolean> delByIds(@PathVariable Long[] ids) {
        applyAppCommonProblemService.removeByIds(Arrays.asList(ids));
        return new IResponse<Boolean>().setMsg("删除数据成功！");
    }

    @PostMapping("/getAppFeedback")
    @ApiOperation(value = "分页查询app反馈意见列表信息")
    public IResponse<IPage<ApplyAppOpinionFeedback>> getAppFeedback(@RequestBody ApplyAppOpinionFeedbackCondition condition) {
        IPage<ApplyAppOpinionFeedback> list = applyAppOpinionFeedbackService.page(new Page(condition.getPageNumber(), condition.getPageSize())
                , Wrappers.<ApplyAppOpinionFeedback>query().lambda()
                        .eq(StringUtils.isNotEmpty(condition.getFeedBackType()), ApplyAppOpinionFeedback::getFeedBackType, condition.getFeedBackType()));
        return IResponse.success(list);
    }

    /**
     * 新增/修改
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveFeedback")
    @CacheEvict(value = "apply_app_opinion_feedback", allEntries = true)
    @ApiOperation(value = "保存修改数据数据")
    public IResponse saveFeedback(@RequestBody ApplyAppOpinionFeedbackCondition condition) {
        applyAppOpinionFeedbackService.saveOrUpdate(condition);
        if(condition.getFile()!=null && condition.getFile().length>0){
            applyAppOpinionFeedbackService.attachmentUpload(condition);
        }
        return IResponse.success("保存成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delFeedbackByIds/{ids}")
    @ApiOperation(value = "批量删除")
    public IResponse<Boolean> delFeedbackByIds(@PathVariable Long[] ids) {
        applyAppOpinionFeedbackService.removeByIds(Arrays.asList(ids));
        return new IResponse<Boolean>().setMsg("删除数据成功！");
    }

    @PostMapping("/getPictureManagement")
    @ApiOperation(value = "分页查询app图片管理列表信息")
    public IResponse<IPage<ApplyAppPictureManagement>> getPictureManagement(@RequestBody ApplyAppPictureManagementCondition condition) {
        IPage<ApplyAppPictureManagement> list = applyAppPictureManagementService.page(new Page(condition.getPageNumber(), condition.getPageSize())
                , Wrappers.<ApplyAppPictureManagement>query().lambda()
                        .eq(StringUtils.isNotEmpty(condition.getPictureType()), ApplyAppPictureManagement::getPictureType, condition.getPictureType()));
        return IResponse.success(list);
    }

    /**
     * 新增/修改
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/savePictureManagement")
    @CacheEvict(value = "apply_app_picture_management", allEntries = true)
    @ApiOperation(value = "保存修改数据数据")
    public IResponse savePictureManagement(@RequestBody ApplyAppPictureManagementCondition condition) {
        applyAppPictureManagementService.saveOrUpdate(condition);
        return IResponse.success("保存成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delPictureManagement/{ids}")
    @ApiOperation(value = "批量删除")
    public IResponse<Boolean> delPictureManagement(@PathVariable Long[] ids) {
        applyAppPictureManagementService.removeByIds(Arrays.asList(ids));
        return new IResponse<Boolean>().setMsg("删除数据成功！");
    }

    @PostMapping("/getVersionInfo")
    @ApiOperation(value = "分页查询app版本列表信息")
    public IResponse<IPage<ApplyAppVersionInfo>> getVersionInfo(@RequestBody ApplyAppVersionInfoCondition condition) {
        IPage<ApplyAppVersionInfo> list = applyAppVersionInfoService.page(new Page(condition.getPageNumber(), condition.getPageSize())
                , Wrappers.<ApplyAppVersionInfo>query().lambda()
                        .eq(StringUtils.isNotEmpty(condition.getOperatingSystem()), ApplyAppVersionInfo::getOperatingSystem, condition.getOperatingSystem()));
        return IResponse.success(list);
    }

    @PostMapping("/getVersionInfoOne")
    @ApiOperation(value = "分页查询app版本列表信息")
    public IResponse <ApplyAppVersionInfo> getVersionInfoOne(@RequestBody ApplyAppVersionInfoCondition condition) {
        List<ApplyAppVersionInfo> list = applyAppVersionInfoService.list(Wrappers.<ApplyAppVersionInfo>query().lambda()
                .eq(ApplyAppVersionInfo::getOperatingSystem, condition.getOperatingSystem())
                .orderByDesc(ApplyAppVersionInfo::getCreateTime));
        if(list !=null && list.size()>0){
            return IResponse.success(list.get(0));
        }else{
            return IResponse.success(list);
        }
    }

    /**
     * 新增/修改
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveVersionInfo")
    @CacheEvict(value = "apply_app_version_info", allEntries = true)
    @ApiOperation(value = "保存修改数据")
    public IResponse saveVersionInfo(@RequestBody ApplyAppVersionInfoCondition condition) {
        applyAppVersionInfoService.saveOrUpdate(condition);
        return IResponse.success("保存成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delVersionInfo/{ids}")
    @ApiOperation(value = "批量删除")
    public IResponse<Boolean> delVersionInfo(@PathVariable Long[] ids) {
        applyAppVersionInfoService.removeByIds(Arrays.asList(ids));
        return new IResponse<Boolean>().setMsg("删除数据成功！");
    }

    @PostMapping("/getAnnouncementInfo")
    @ApiOperation(value = "分页查询app公告列表信息")
    public IResponse<IPage<TsysNoticePublish>> getVersionInfo(@RequestBody TsysNoticePublishCondition condition) {
        IPage<TsysNoticePublish> list = tsysNoticePublishService.page(new Page(condition.getPageNumber(), condition.getPageSize())
                , Wrappers.<TsysNoticePublish>query().lambda()
                        .eq(StringUtils.isNotEmpty(condition.getChannelBelong()), TsysNoticePublish::getChannelBelong, condition.getChannelBelong())
                        .eq(StringUtils.isNotEmpty(condition.getIsUrgent()), TsysNoticePublish::getIsUrgent, condition.getIsUrgent()));
        return IResponse.success(list);
    }

    /**
     * 新增/修改
     *
     * @param condition
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveAnnouncementInfo")
    @CacheEvict(value = "tsys_notice_publish", allEntries = true)
    @ApiOperation(value = "保存修改数据")
    public IResponse saveAnnouncementInfo(@RequestBody TsysNoticePublishCondition condition) {
        tsysNoticePublishService.saveOrUpdate(condition);
        return IResponse.success("保存成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/delAnnouncementInfo/{ids}")
    @ApiOperation(value = "批量删除")
    public IResponse<Boolean> delAnnouncementInfo(@PathVariable Long[] ids) {
        tsysNoticePublishService.removeByIds(Arrays.asList(ids));
        return new IResponse<Boolean>().setMsg("删除数据成功！");
    }

    @PostMapping("/getPictureManagementOne")
    @ApiOperation(value = "获取图片类型")
    public IResponse <ApplyAppPictureManagement> getPictureManagementOne(@RequestBody ApplyAppPictureManagementCondition condition) {
        List<ApplyAppPictureManagement> list = applyAppPictureManagementService.list(Wrappers.<ApplyAppPictureManagement>query().lambda()
                .eq(ApplyAppPictureManagement::getPictureType, condition.getPictureType())
                .orderByAsc(ApplyAppPictureManagement::getPictureIndex));
        List<ComAttachmentFile> comAttachmentFileList = new ArrayList<>();
        if(list !=null && list.size()>0){
            for(ApplyAppPictureManagement management :list){
                List<ComAttachmentFile> fileList = attachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                        .eq(ComAttachmentFile::getBusiNo, management.getId().toString()).orderByDesc(ComAttachmentFile::getCreateTime));
                if(fileList !=null && fileList.size()>0){
                    comAttachmentFileList.add(fileList.get(0));
                }
            }
        }else{
            return IResponse.fail("暂无数据");
        }
        return IResponse.success(comAttachmentFileList);
    }
}
